<?php


class wikigallery extends WikiPlugin
{
	function wikigallery()
	{
		$this->add_macro('gallery', 'gallery');
		$this->add_macro('special:gallerydropdown', 'gallerydropdown');
		$this->accumulate('header', 'prototype', 'prototype');
		$this->accumulate('header', 'scriptaculous', 'scriptaculous');
		$this->accumulate('header', 'lightbox', 'lightbox');
		$this->add_ajax_handler('gallery', 'ajax');
	}
	
	function gallerydropdown()
	{
		global $mwconfig, $rewrite, $wiki;

		$d = dir("{$mwconfig->imagedir}");
		$dirs = array();
		while (false !== ($file = $d->read()))
			if(is_dir("{$mwconfig->imagedir}/{$file}") && $file{0} != '.') $dirs[] = $file;
		$d->close();
		$output = '<select name="gallery" onchange="' . $params[1] . '">';
		$output .= '<option value="">--None--</option>';
		$output .= '<option value="/new" style="background-color:#eeffee;">--New Gallery--</option>';
		foreach($dirs as $dir) {
			$output .="<option value=\"{$dir}\">{$dir}</option>";
		}
		$output .= '</select>';
		return $output;
	}
	
	function gallery()
	{
		global $mwconfig, $rewrite, $wiki;

		$params = func_get_args();
		$gallerydir = array_shift($params);

		if(is_dir("{$mwconfig->imagedir}/{$gallerydir}"))
		{
			$d = dir("{$mwconfig->imagedir}/{$gallerydir}");
			$images = array();
			while (false !== ($file = $d->read()))
				if($file{0} != '.') $images[] = $file;
			$d->close();

			// To do: Update gallery parameter settings
			$size = 'thumb';
			extract($this->process_params(array(
				'size'=>'/[0-9]{1,3}x[0-9]{1,3}/i',
				'showurls'=>'/urls/i',
			), $params));
			$showurls = ($showurls != '');
			
			$ngallerydir = preg_replace('/[^a-z_0-9]+/', '_', $gallerydir);

			$ret = '
				<div class="wiki-gallery" id="wiki_gallery_' . $ngallerydir . '">
			';
			foreach($images as $image)
			{
				$viewimage = $wiki->imagetag("{$gallerydir}/{$image}", array($size, 'urlonly'));
				$imageurl = $wiki->imagetag($gallerydir . '/' . $image, array('urlonly'));			
				$ret .= "<a href=\"{$imageurl}\" class=\"wikiclickimage\" rel=\"lightbox[gallery]\" title=\"@({$gallerydir}/{$image})\"><img src=\"{$viewimage}\" alt=\"\"/></a>\n";				
				$wiki->linksto[] = "Media" . SPECIAL_SEPARATOR . "{$gallerydir}/{$image}";
			}
			$ret .= '</div>';
		}
		else
		{
			$ret = '<div>No gallery named "' . $gallerydir . '" exists here.</div>';
		}
		return "<nopee>{$ret}</nopee>";
	}

	function prototype()
	{
		global $rewrite, $wiki;

		if(preg_match('/{{gallery:.+?}}/i',$wiki->page_rec->body)) {
			return '<script type="text/javascript" src="' . $rewrite->get_link(array('page'=>'js/prototype.js')) . '"></script>';
		}
		return '';
	}
	
	function scriptaculous()
	{
		global $rewrite, $wiki;
		
		if(preg_match('/{{gallery:.+?}}/i',$wiki->page_rec->body)) {
			return '<script type="text/javascript" src="' . $rewrite->get_link(array('page'=>'js/scriptaculous.js')) . '"></script>';
		}
		return '';
	}
	
	function lightbox()
	{
		global $rewrite, $wiki;
		
		if(preg_match('/{{gallery:.+?}}/i',$wiki->page_rec->body)) {
			return '
			<script type="text/javascript">var ajaxurl="' . $rewrite->get_link(array('special'=>'ajax')) . '";</script>
			<script type="text/javascript" src="' . $rewrite->get_link(array('page'=>'js/lightbox.js')) . '"></script>
			<link rel="stylesheet" type="text/css" href="' . $rewrite->get_link(array('page'=>'plugins/gallery/lightbox.css')) . '" media="screen" />';
		}
		return '';
	}
	
	function ajax()
	{
	  global $wiki, $mwconfig, $db, $session;
	  
	  $image = $_POST['image'];

		$imageurl = $wiki->imagetag($image, array('urlonly'));			
		$mediaurl = $wiki->imagetag($image, array('urlonly','media'));
	  
		$nw = new wikiengine();
		$pg = $nw->getpage("useformat=&pagename=Media:" . $image, $nil, false);
	  
		echo '
			<script type="text/javascript">
			detailtoggler = function() {
				detailtoggled = !detailtoggled;
				if(detailtoggled) {
					Effect.SlideDown(\'wikiimagedetail\');
				}
				else {
					Effect.SlideUp(\'wikiimagedetail\');
				}
			}
			</script>
			<a href="#" onclick="detailtoggler()">Toggle details for ' . basename($image) . '</a>
			<div id="wikiimagedetail" style="display:none"><br/>
				<div><small><a href="' . $mediaurl . '">wiki page for ' . basename($image) . '</a> 
				<a href="' . $imageurl . '">raw image</a></small><br/>
				' . $pg . '
				</div>
			</div>
			<script type="text/javascript">if(!detailtoggled)Element.hide(\'wikiimagedetail\');else Element.show(\'wikiimagedetail\');</script>
		';
	}
}

?>
